﻿using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.Tools.Editor
{
    public class PrefabReplaceEditor : EditorWindow
    {
        // [MenuItem("MENUITEM/MENUITEMCOMMAND")]
        // private static void ShowWindow()
        // {
        //     var window = GetWindow<PrefabReplaceEditor>();
        //     window.titleContent = new GUIContent("TITLE");
        //     window.Show();
        // }

        [MenuItem("Window/PrefabReplaceTool")]
        private static void Init()
        {
            var windows = (PrefabReplaceEditor[])Resources.FindObjectsOfTypeAll(typeof(PrefabReplaceEditor));
            if (windows.Length == 0)
            {
                PrefabReplaceEditor window = (PrefabReplaceEditor)GetWindow(typeof(PrefabReplaceEditor));
                window.InitInternal();
                

            }
            else
            {
                FocusWindowIfItsOpen(typeof(PrefabReplaceEditor));
            }
        }

        private void OnEnable()
        {
            titleContent = new GUIContent("PrefabReplaceTool");
            minSize = new Vector2(200,200);
        }
        
        public void InitInternal()
        {
            
        }

        private GameObject _prefab;
        
        private void OnGUI()
        {
            GameObject pObj = EditorGUILayout.ObjectField("Prefab", _prefab, typeof(GameObject)) as GameObject;
            if (pObj != null && PrefabUtility.GetPrefabAssetType(pObj) != PrefabAssetType.NotAPrefab)
            {
                _prefab = pObj;
            }
            else
            {
                _prefab = null;
            }

            if (GUILayout.Button("替换"))
            {
                //Undo.RecordObjects(target, "Changed Prefab Instance");
                if (Selection.gameObjects != null)
                {
                    foreach (var sg in Selection.gameObjects)
                    {
                        GameObject p = PrefabUtility.GetCorrespondingObjectFromSource(sg);
                        if (p)
                        {
                            var pInstance = PrefabUtility.InstantiatePrefab(_prefab) as GameObject;
                            pInstance.transform.SetParent(sg.transform.parent);
                            pInstance.transform.SetSiblingIndex(sg.transform.GetSiblingIndex());
                            pInstance.transform.localPosition = sg.transform.localPosition;
                            pInstance.transform.localRotation = sg.transform.localRotation;
                            pInstance.transform.localScale = sg.transform.localScale;
                            Undo.RegisterCreatedObjectUndo (pInstance, "Create Cube");
                            Undo.DestroyObjectImmediate(sg);
                            //GameObject.DestroyImmediate(sg);
                        }
                    }
                    
                }
                //UnityEngine.Debug.LogError("替换");
            }
        }
        
        
        
    }
}